package edu.ncsu.helloandroid;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;

/**
 * Main entry point for Disc Golf Caddie.. should always be root task for application.
 * 
 * @author Matthew Marum
 *
 */
public class FGCMain extends FGCActivity {

	private FrameLayout frame = null;

	private Button playButton = null;
	private Button scorecardButton = null;
	
	public static Intent FGCOURSE_DEFAULT_INTENT = null;
	private FGCGameState state = null;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        state = FGCGameState.getGameState(getTaskId());
        
        if(FGCOURSE_DEFAULT_INTENT == null){
        	FGCOURSE_DEFAULT_INTENT = new Intent(this,FGCourse.class);
        }
        frame = new FrameLayout(this);

        final ProgressDialog dlg =  ProgressDialog.show(this, getResources().getText(R.string.title), "Locating player...", true, false);
        
        Handler msgHandler = new Handler();
        msgHandler.postDelayed(new Runnable() {
			
			public void run() {
				dlg.setMessage("Finding course...");
				
			}
		}, 3000);
        msgHandler.postDelayed(new Runnable() {
			
			public void run() {
				dlg.dismiss();
				createMainMenuContent();
			}
		}, 5000);
       
        

    }

	private void createMainMenuContent() {
		View.inflate(this, R.layout.main_menu, frame);
        setContentView(frame);

        playButton = (Button) findViewById(R.id.StartButton);
        playButton.setText("Start Game!");
        playButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				startActivityIfNeeded(FGCOURSE_DEFAULT_INTENT, CREATE_COURSE_ACTIVITY);
				if(!state.isPlaying()){
					Log.i(FGCMain.class.toString(), "Starting Game!!");
					state.setPlaying(true);
				}
			}
			
		});
        
        scorecardButton = (Button) findViewById(R.id.ScorecardMainMenuButton);
        scorecardButton.setVisibility(state.isPlaying()? View.VISIBLE : View.INVISIBLE);
        scorecardButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Intent scorecardIntent = new Intent(FGCMain.this,FGCScorecard.class);
				startActivityForResult(scorecardIntent,CREATE_SCORECARD_ACTIVITY);
			}
		});
        
        findViewById(R.id.OnlineMainMenuButton).setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				
				Intent onlineIntent = new Intent(FGCMain.this,FGCOnline.class);
				startActivityForResult(onlineIntent, CREATE_SOCIAL_NETWORK_ACTIVITY);
				
			}
		});
        findViewById(R.id.QuitMainMenuButton).setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				finish();
				
			}
		});
        
	}
    
    @Override
    protected void onResume() {
    	if(playButton != null){
	    	if(state.isPlaying()){
				playButton.setText("Resume..."); 		
				scorecardButton.setVisibility(View.VISIBLE);
	    	} else {
	    		playButton.setText("Start Game!");
	    		scorecardButton.setVisibility(View.INVISIBLE);
	    	}
    	}
    	super.onResume();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	//Blank options menu.. all options visible on Main Menu.
    	return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    protected void onDestroy() {
    	FGCGameState.purgeGameState(getTaskId());
    	super.onDestroy();
    }
    
	protected boolean showScorecardItem() {
		return false;
	}

    
}